package com.quanxiaoha.weblog.web.controller.card;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quanxiaoha.weblog.common.aspect.ApiOperationLog;
import com.quanxiaoha.weblog.common.domain.entity.Article;
import com.quanxiaoha.weblog.common.domain.entity.ArticleContent;
import com.quanxiaoha.weblog.common.domain.entity.Category;
import com.quanxiaoha.weblog.common.domain.query.ArticleListQuery;
import com.quanxiaoha.weblog.common.domain.query.ArticleQuery;
import com.quanxiaoha.weblog.common.domain.query.CategoryQuery;
import com.quanxiaoha.weblog.common.domain.query.CountQuery;
import com.quanxiaoha.weblog.common.domain.vo.ArticleCategoryRelVo;
import com.quanxiaoha.weblog.common.domain.vo.ArticleVo;
import com.quanxiaoha.weblog.common.domain.vo.CategoryVo;
import com.quanxiaoha.weblog.common.domain.vo.ReadNumVo;
import com.quanxiaoha.weblog.common.service.IScheduleArticleService;
import com.quanxiaoha.weblog.common.utils.Response;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author Roy
 * @since 2025-06-02
 */
@RestController
@RequestMapping("/todo")
public class ScheduleArticleController {

    @Autowired
    IScheduleArticleService scheduleArticleService;

    @GetMapping("/categoryList")
    @ApiOperationLog(description = "卡片库列表")
    @ApiOperation(value = "卡片库列表")
    public Response categoryList() {

        List<CategoryVo> categories = scheduleArticleService.categoryList();
        return Response.success(categories);
    }

    @GetMapping("/getNumList")
    @ApiOperationLog(description = "统计数据")
    @ApiOperation(value = "统计数据")
    public Response getNumList() {

        List<ArticleCategoryRelVo> numList = scheduleArticleService.getNumList();
        return Response.success(numList);
    }


    @PostMapping("/saveCategory")
    @ApiOperationLog(description = "保存卡片库")
    @ApiOperation(value = "保存卡片库")
    public Response addCategory(@RequestBody CategoryQuery categoryquery) {

        scheduleArticleService.saveCategory(categoryquery);
        return Response.success();
    }


    @DeleteMapping("/deleteCategory/{id}")
    @ApiOperationLog(description = "删除卡片库")
    @ApiOperation(value = "删除卡片库")
    public Response deleteCategory(@PathVariable Integer id) {

        scheduleArticleService.deleteCategory(id);
        return Response.success();
    }

    @PostMapping("/articleList")
    @ApiOperationLog(description = "卡片列表")
    @ApiOperation(value = "卡片列表")
    public Response articleList(@RequestBody @Validated ArticleListQuery query) {

        Page<ArticleVo> articles = scheduleArticleService.articleList(query);
        return Response.success(articles);
    }

    @PostMapping("/saveArticle")
    @ApiOperationLog(description = "保存卡片")
    @ApiOperation(value = "保存卡片")
    public Response saveArticle(@RequestBody ArticleQuery article) {

        ArticleVo articleVo = scheduleArticleService.saveArticle(article);
        return Response.success(articleVo);
    }

    @DeleteMapping("/deleteArticle/{id}")
    @ApiOperationLog(description = "删除卡片")
    @ApiOperation(value = "删除卡片")
    public Response deleteArticle(@PathVariable Integer id) {

        scheduleArticleService.deleteArticle(id);
        return Response.success();
    }

    @GetMapping("/clock/{id}")
    @ApiOperationLog(description = "打卡")
    @ApiOperation(value = "打卡")
    public Response clock(@PathVariable Integer id) {

        ReadNumVo readNumVo = scheduleArticleService.clock(id);
        return Response.success(readNumVo);
    }

    @PostMapping("/upload")
    @ApiOperationLog(description = "上传图片")
    @ApiOperation(value = "上传图片")
    public Response upload(@RequestParam("file") MultipartFile file) {

        try {
            String upload = scheduleArticleService.upload(file);
            return Response.success(upload);
        } catch (Exception e) {
            return Response.fail(e.getMessage());
        }
    }


    @GetMapping("/getCategory")
    @ApiOperationLog(description = "随机卡片")
    @ApiOperation(value = "随机卡片")
    public Response getCategory() {

        ArticleVo articleContent = scheduleArticleService.getCategory();
        return Response.success(articleContent);
    }

}
